package com.cmpan.networkdisk.controller.admin;

import com.cmpan.networkdisk.model.Type;
import com.cmpan.networkdisk.model.User;
import com.cmpan.networkdisk.service.TypeService;
import com.cmpan.networkdisk.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * ClassName:AdminDiskController
 * Package:com.cmpan.networkdisk.controller.admin
 * Description:
 *
 * @Date:2020/7/14 14:20
 * @com.chuangmei
 */
@Controller
@RequestMapping("/admindisk")
public class AdminDiskController {

    @Autowired
    private TypeService typeService;


    @GetMapping("/type")
    public String goType(HttpServletRequest request, Model model){

        //user
        User user = (User) request.getSession().getAttribute("user");
        //查询所有的类型
        List<Type> types = typeService.getAllType();

        //addAttrrbute
        model.addAttribute("types",types);
        model.addAttribute("user",user);

        return "admin/inputType";
    }

    @GetMapping("/edittype")
    public String editType(HttpServletRequest request,String id,Model model){

        //通过传入的id查询type
        Type type = typeService.queryByPrimary(Long.valueOf(id));
        //加入request
        model.addAttribute("type",type);

        return "admin/editorType";
    }

    @PostMapping("/editortype")
    public String editorType(Type type , HttpServletRequest request){

        int count = typeService.updateByPrimary(type);

        return count > 0 ? "redirect:/admindisk/type" : "error/error";
    }

    @GetMapping("/deletetype")
    public String deleteType(String id, HttpServletRequest request){

        int count = typeService.deleteByPrimary(Long.valueOf(id));

        return count > 0 ? "redirect:/admindisk/type" : "error/error";
    }

    @GetMapping("/addtype")
    public String addType(){
        return "admin/addType";
    }

    @PostMapping("/newtype")
    public String newType(Type type){
       int count =  typeService.addType(type);
       return count > 0 ? "redirect:/admindisk/type" : "error/error";
    }
}
